package org.anyline.ctrl;

import org.anyline.controller.impl.AnylineController;
import org.anyline.entity.DataRow;
import org.anyline.entity.DataSet;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller("DefaultController")
@RequestMapping({"/"})
public class DefaultController extends AnylineController
{
  @RequestMapping({"index"})
  public ModelAndView index()
  {
    ModelAndView mv = new ModelAndView("index");
    return mv;
  }

  @RequestMapping({"q"})
  public ModelAndView queryr()
  {
    ModelAndView mv = new ModelAndView("query");
    DataSet set = this.service.query("member", parseConfig(true,  "NM:%nm%", "AGE:>=ageFr", "AGE:<=ageTo", "TE:tel" ));
    mv.addObject("set", set);
    return mv;
  }

  @RequestMapping({"a"})
  public ModelAndView add()
  {
    ModelAndView mv = new ModelAndView("info");
    return mv;
  }

  @RequestMapping({"u"})
  public ModelAndView update()
  {
    ModelAndView mv = new ModelAndView("info");
    DataRow row = this.service.queryRow("member", parseConfig( "+CD:cd" ));
    mv.addObject("row", row);
    return mv;
  }
  @RequestMapping({"v"})
  public ModelAndView view() {
    ModelAndView mv = new ModelAndView("view");
    DataRow row = this.service.queryRow("member", parseConfig( "+CD:cd" ));
    mv.addObject("row", row);
    return mv;
  }

  @RequestMapping({"s"})
  public ModelAndView save()
  {
    ModelAndView mv = new ModelAndView("redirect:q");
    DataRow row = entityRow( "CD:cd", "NM:nm", "AGE:age", "TEL:tel" ).addPrimaryKey("CD" );
    this.service.save("member", row);
    System.out.println("新添加一条记录,主键=" + row.getString("CD"));
    return mv;
  }

  @RequestMapping({"d"})
  @ResponseBody
  public String delete()
  {
    if (!checkRequired(new String[] { "编号:cd" })) {
      return fail();
    }
    DataRow row = this.service.queryRow("member", parseConfig( "+CD:cd"));
    if (row == null) {
      return fail("数据不存在");
    }
    this.service.delete(row, new String[0]);
    return success();
  }
}